﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace mogco.Visuals
{
    public partial class ProcessWindow : Form
    {
        delegate void SetCloseStateDelegate(bool value);
        private delegate void WriteMessageDelegate(string message);
        private delegate void SetProgressDelegate(int value);
        public ProcessWindow()
        {
            InitializeComponent();
        }

        public void WriteMessage(string message)
        {
            if (listBox1.InvokeRequired)
            {
                WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
                this.BeginInvoke(d, new object[] { message });
            }
            else
            {
                listBox1.Items.Add(message + Environment.NewLine);
                listBox1.SelectedIndex = listBox1.Items.Count-1;
            }

        }

        public void SetProgress(int value)
        {
            if (progressBar1.InvokeRequired)
            {
                SetProgressDelegate d = new SetProgressDelegate(SetProgress);
                this.Invoke(d, new object[] { value } );
            }
            else
            {
                progressBar1.Value = value;
            }
        }

        public void SetProgressBound(int min, int max)
        {
            progressBar1.Maximum = max;
            progressBar1.Minimum = min;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 0;
            this.Close();
        }

        public void SetCloseState(bool value)
        {
            if (button1.InvokeRequired)
            {
                SetCloseStateDelegate scsd = new SetCloseStateDelegate(SetCloseState);
                button1.Invoke(scsd, new Object[]{value});
            }
            else
                button1.Enabled = value;
        }

        private void ProcessWindow_FormClosed(object sender, FormClosedEventArgs e)
        {
            listBox1.Items.Clear();
        }
    }
}
